package com.hserver.statistics.util;


import com.hserver.statistics.bean.Menu;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class MenuUtil {

    /**
     * 生成菜单
     */
    public static List<Menu> generateMenu() {
        String runPath = FileUtil.getRunPath();
        if (runPath == null) {
            return null;
        }
        String s = runPath + File.separator + "db" + File.separator;
        if (new File(s).isDirectory()) {
            List<Menu> menus1 = new ArrayList<>();
            //第一层父级小程序;
            File oneFile = new File(s);
            String[] list = oneFile.list();
            if (list == null) {
                return null;
            }
            for (String s1 : list) {
                Menu menu = new Menu();
                menu.setName(s1);
                menus1.add(menu);
            }
            //处理二级文件夹
            for (String s1 : list) {
                String s2 = s + s1 + File.separator;
                File twoFile = new File(s2);
                if (twoFile.isDirectory()) {
                    //添加二级菜单
                    for (int i = 0; i < menus1.size(); i++) {
                        Menu menu = menus1.get(i);
                        if (menu.getName().equals(s1)) {
                            String[] list1 = twoFile.list();
                            List<Menu> menus2 = new ArrayList<>();
                            for (String s3 : list1) {
                                Menu menu1 = new Menu();
                                menu1.setName(s3);
                                menus2.add(menu1);
                            }
                            menu.setMenus(menus2);
                            menus1.set(i, menu);
                        }
                    }
                }
            }
            //处理第三次文件夹
            for (int i = 0; i < menus1.size(); i++) {
                Menu menu = menus1.get(i);
                List<Menu> menus = menu.getMenus();
                for (int j = 0; j < menus.size(); j++) {
                    Menu menu2 = menus.get(j);
                    String s1 = s + menu.getName() + File.separator + menu2.getName() + File.separator;
                    File file = new File(s1);
                    if (file.isDirectory()) {
                        String[] list1 = file.list();
                        List<Menu> menus2 = new ArrayList<>();
                        for (String s2 : list1) {
                            Menu menu1 = new Menu();
                            String fileName = s2.substring(0, s2.lastIndexOf("."));
                            String name = fileName.substring(0, fileName.length() - 8);
                            int ii = name.lastIndexOf("-");
                            String vx = name.substring(0, ii);
                            menu1.setName(vx);
                            try {
                                menu1.setUrl(URLEncoder.encode(s1+vx, "UTF-8").trim());
                            } catch (Exception e) {
                            }
                            boolean temp = true;
                            for (Menu menu3 : menus2) {
                                if (menu3.getName().equals(menu1.getName())) {
                                    temp = false;
                                }
                            }
                            if (temp) {
                                menus2.add(menu1);
                            }
                        }
                        menu2.setMenus(menus2);
                        menus.set(j, menu2);
                    }
                }
            }
            return menus1;
        }
        return null;
    }

}
